package com.gopay.bis.cmbc.expay.integrate;

import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;

/**
 * 
 * @author lizheng
 *
 */
public class PostOffice implements BeanNameAware {

	protected String beanName;

	public String getBeanName() {
		return this.beanName;
	}

	@Override
	public void setBeanName(String name) {
		this.beanName = name;
	}

	/**
	 * 日志对象
	 */
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 发送队列
	 */
	private LinkedBlockingQueue<Letter> sendQueue = new LinkedBlockingQueue<Letter>();

	/**
	 * 接收队列
	 */
	private LinkedBlockingQueue<Letter> receiveQueue = new LinkedBlockingQueue<Letter>();

	public void putToSendQueue(Letter letter) throws InterruptedException {
		logger.info("{} putToSendQueue, currnet size:{}", beanName, sendQueue.size());
		sendQueue.put(letter);
	}

	public Letter takeFromSendQueue() throws InterruptedException {
		logger.info("{} takeFromSendQueue, currnet size:{}", beanName, sendQueue.size());
		return sendQueue.take();
	}

	public void putToReceiveQueue(Letter letter) throws InterruptedException {
		logger.info("{} putToReceiveQueue, currnet size:{}", beanName, receiveQueue.size());
		receiveQueue.put(letter);
	}

	public Letter takeFromReceiveQueue() throws InterruptedException {
		logger.info("{} takeFromReceiveQueue, currnet size:{}", beanName, receiveQueue.size());
		return receiveQueue.take();
	}

}
